#!/bin/bash

project_list=(
    "git/git"
    "stedolan/jq"
    "houtianze/bypy"
    "apolloconfig/apollo"
    "alibaba/nacos"
    "pinpoint-apm/pinpoint"
    "jenkinsci/jenkins"
    "NewFuture/DDNS"
    "fatedier/frp"
    "nginx/nginx"
    "PhilipHazel/pcre2"
)

check_url="www.baidu.com"
github_url="https://github.com"
github_proxy_url="https://ghproxy.com"
BASE_PATH=$(
    cd $(dirname $0)
    pwd
)

install_dir=/opt
[[ "0" != "$(id -u)" ]] && install_dir=${HOME}
SoftWarePackages_dir=${install_dir}/SoftWarePackages
[[ ! -e ${SoftWarePackages_dir} ]] && mkdir -p ${SoftWarePackages_dir}

tag_index=""
tag_name=""
file_download_url=""

Font="\e[0m"
Font_Red="\e[31m"
Font_Green="\e[32m"
Font_yellow="\e[33m"
Font_Blue="\e[34m"
Font_Purple="\e[35m"
Font_lBlue="\e[36m"

function echoRed() {
    echo -e "${Font_Red}${1}${Font}"
}
function echoGreen() {
    echo -e "${Font_Green}${1}${Font}"
}
function echolBlue() {
    echo -e "${Font_lBlue}${1}${Font}"
}
function echoYellow() {
    echo -e "${Font_yellow}${1}${Font}"
}
function printflBlue() {
    #echo -e "${Font_lBlue}${1} ${2}${Font}"
    printf "${Font_lBlue}[%s] %-25s${Font}\n" ${1} ${2}
}

function getTagIndex() {
    local tag_list=(${1})
    if [ 0 != ${#tag_list[@]} ]; then
        for i in $(seq 1 ${#tag_list[@]}); do
            printflBlue ${i} ${tag_list[$i - 1]}
        done
    else
        echoRed "获取版本号失败。"
        exit 1
    fi

    default_num=1
    read -p "请输入安装版本的序号，默认: ${default_num}。输入:" tag_num

    if [ -n "$tag_num" ]; then
        if [ -z "${tag_num//[0-9]/}" ]; then
            if [[ ${tag_num} -lt 1 || ${#tag_list[@]} -lt ${tag_num} ]]; then
                echoRed "输入的数字介于1-${#tag_list[@]}"
                exit 1
            fi
        else
            echoRed "输入的不是纯数字"
            exit 1
        fi
    else
        tag_num=${default_num}
        echo "使用默认值${default_num} ${tag_num}"
    fi
    tag_index=$(expr $tag_num - 1)
    tag_name=${tag_list[${tag_index}]}
}

function getFileDownloadUrl() {
    local file_download_url_list=(${1})
    if [ 0 != ${#file_download_url_list[@]} ]; then
        for i in $(seq 1 ${#file_download_url_list[@]}); do
            printflBlue ${i} ${file_download_url_list[$i - 1]##*/}
        done
    else
        echoRed "获取下载的文件列表失败"
        exit 1
    fi

    printf "请输入要下载的项目的的序号，默认: ${Font_Red}退出！${Font}。"
    read -p "输入:" fnum

    if [ -n "${fnum}" ]; then
        if [ -z "${fnum//[0-9]/}" ]; then
            if [[ ${fnum} -lt 1 || ${#file_download_url_list[@]} -lt ${fnum} ]]; then
                echo "输入的数字介于1-${#file_download_url_list[@]}"
                exit 1
            fi
        else
            echoRed "输入的不是纯数字"
            exit 1
        fi
    else
        exit 0
    fi
    file_download_url="${file_download_url_list[$(expr ${fnum} - 1)]}"
}


####################开始####################
ping -c 4 ${check_url} &>/dev/null
if [[ 0 != $? ]]; then
    echoRed "未连接到互联网"
    exit 1
fi

which jq >/dev/null 2>&1
if [[ $? != 0 ]];then
    curl -sS -o jq https://ghproxy.com/https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
    sudo chmod +x jq
    sudo cp jq /usr/bin/jq
    
fi

echoYellow "================项目列表================"
if [ 0 != ${#project_list[@]} ]; then
    for i in $(seq 1 ${#project_list[@]}); do
        printflBlue ${i} ${project_list[$i - 1]}
    done
else
    echoRed "获取项目列表为空。"
    exit 1
fi

printf "请输入要下载的项目的的序号，默认: ${Font_Red}退出！${Font}。"
read -p "输入:" project_num

if [ -n "${project_num}" ]; then
    if [ -z "${project_num//[0-9]/}" ]; then
        if [[ ${project_num} -lt 1 || ${#project_list[@]} -lt ${project_num} ]]; then
            echoRed "输入的数字介于1-${#project_list[@]}"
            exit 1
        fi
    else
        echoRed "输入的不是纯数字"
        exit 1
    fi
else
    exit 1
fi

project_name="${project_list[$project_num - 1]}"
get_releases_url="https://api.github.com/repos/${project_name}/releases"
get_tags_url="https://api.github.com/repos/${project_name}/tags"

echoYellow "正在请求项目的release信息"
responce_releases=$(curl -s -H "Accept: application/vnd.github.v3+json" ${get_releases_url} | jq .)
responce_tags=""
if [[ "" == "${responce_releases}" || "[]" == "${responce_releases}" ]]; then
    responce_tags=$(curl -s -H "Accept: application/vnd.github.v3+json" ${get_tags_url} | jq .)
    if [[ "" == "${responce_tags}" || "[]" == "${responce_tags}" ]]; then
        echoRed "未获取到releases或tags信息"
        exit 1
    fi
    tag_list=($(echo ${responce_tags} | jq .[].name))
    getTagIndex "$(echo \"${tag_list[@]}\" | sed 's/\"//g')"
    #filename_list=($(echo ${responce_tags} | jq .[${tag_index}]))
    file_download_url="${github_url}/${project_name}/archive/refs/tags/${tag_name}.tar.gz"
else
    tag_list=($(echo ${responce_releases} | jq .[].tag_name))
    getTagIndex "$(echo \"${tag_list[@]}\" | sed 's/\"//g')"
    # 下面这一行神了，不懂原理，有知道的求教!!! 手机:15245772130
    # tag_index=$(getTagIndex "$(echo \"${tag_list[@]}\" | sed 's/\"//g')")
    file_download_url_list=($(echo ${responce_releases} | jq .[${tag_index}].assets[].browser_download_url))
    # 下边这一行也是!!!
    # file_download_url=$(getFileDownloadUrl "$(echo "${file_download_url_list[@]}" | sed 's/\"//g')")
    getFileDownloadUrl "$(echo "${file_download_url_list[@]}" | sed 's/\"//g')"
    
fi

echoYellow "file_download_url:${file_download_url}"

proxy_download_url=${github_proxy_url}/${file_download_url}
echoYellow "proxy_download_url:${proxy_download_url}"

filename=${file_download_url##*/}

download_local_dir="${SoftWarePackages_dir}/${project_name##*/}/${tag_name}"
[[ ! -e ${download_local_dir} ]] && mkdir -p ${download_local_dir}

curl -L# -o ${download_local_dir}/${filename} ${proxy_download_url}

ls -lh ${download_local_dir}/${filename}
